#include <rclcpp/rclcpp.hpp>
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"

#include <image_transport/image_transport.hpp>
#include <cv_bridge/cv_bridge.h>
#include "std_msgs/msg/string.hpp" 
#include <sstream>
#include <iostream>
#include <stdio.h>
#include "sensor_msgs/image_encodings.hpp"

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

class PubNode : public rclcpp::Node
{
public:
    PubNode(std::string name,cv::VideoCapture &cap):Node(name)
    {   
        this->cap=&cap;
        publisher= this->create_publisher<sensor_msgs::msg::Image>("photo", 10);
        timer = this->create_wall_timer(std::chrono::milliseconds(10), std::bind(&PubNode::timer_callback, this));
    }

private:
    void timer_callback()
    {        
        cv_bridge::CvImage cvi;
        sensor_msgs::msg::Image im;
        fp=cap->read(Frame);
        if(fp){
            cvi.image=Frame;cvi.encoding="bgr8";            
            cvi.toImageMsg(im);
            publisher->publish(im);              
        }
    }
    rclcpp::TimerBase::SharedPtr timer;
    rclcpp::Publisher<sensor_msgs::msg::Image>::SharedPtr publisher;
    cv::VideoCapture *cap;cv::Mat Frame;int fp;
};


int main(int argc,char *argv[]){

    setlocale(LC_ALL,"");
    rclcpp::init(argc,argv);

    cv::VideoCapture cap("/home/caishuyang/video/red.avi");
    auto node=std::make_shared<PubNode>("publisherb",cap);
    rclcpp::spin(node);
    rclcpp::shutdown();
}
